package com.koala.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.koala.db.DogOperations;

public class AddDog extends ActionBarActivity {
	
	private DogOperations dogDBoperation;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_dog);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
		/* Dog table operations */
		
		//Create DB object and open DB connection
		dogDBoperation = new DogOperations(this);
		dogDBoperation.open();
		
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_dog, menu);
		return true;
	}

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {
		/*case R.id.action_settings:
			return true;*/
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	
	@Override
	protected void onResume() {
		dogDBoperation.open();
		super.onResume();
	}

	
	@Override
	protected void onPause() {
		dogDBoperation.close();
		super.onPause();
	}
	
	
	/** Add dog to the DB (onClick button)
	 * @param view
	 */
	public void addDog(View view) {
		
		//Retrieve data from input fields
		EditText personPhone = (EditText) findViewById(R.id.personPhoneTxt);
		String personPhoneStr = personPhone.getText().toString();
		int personPhoneInt = Integer.parseInt(personPhoneStr);
		EditText dogName = (EditText) findViewById(R.id.dogNameTxt);
		String dogNameStr = dogName.getText().toString();
		EditText dogBreed = (EditText) findViewById(R.id.dogBreedTxt);
		String dogBreedStr = dogBreed.getText().toString();
		
		//Add dog to DB
		dogDBoperation.addDog(personPhoneInt, dogNameStr, dogBreedStr);
		
		//Return to Main activity
	    Intent mainIntent = new Intent(this, MainActivity.class);
        startActivity(mainIntent);
        
        Toast.makeText(this, dogNameStr+getString(R.string.dog_success_message), Toast.LENGTH_LONG).show();
	   
	}
	
	
	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_add_dog,
					container, false);
			return rootView;
		}
	}

}
